package com.friend.solr.service.impl;

import com.friend.common.model.GenericPage;
import com.friend.common.model.PageResult;
import com.friend.entity.entity.base.FilmInfo;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: friend->SearchServiceImpl
 * @description: Solr搜索服务
 * @author: yangzhanghui
 * @create: 2019-11-22 12:37
 **/
@Service
public class SearchServiceImpl {

    @Autowired
    private SolrClient solrClient;
    //search就是搜索的内容，currentpage是因为我做了分页，如果没做分页可忽略此参数
    public PageResult searchNews(String search, int currentPage) throws IOException, SolrServerException {
        //创建solr查询对象
        SolrQuery query = new SolrQuery();
        if(null != search && !"".equals(search)){
            //设置查询关键词
            query.setQuery("filmDesc:*" + search + "*");
            // 设置默认查询域
            query.set("df", "filmDesc");
        }
        //高亮显示
        query.setHighlight(true);
        // 设置高亮显示字段
        query.addHighlightField("filmName,filmDesc");
        query.setHighlightSimplePre("<span style='color:red'>");
        query.setHighlightSimplePost("</span>");
        //设置排序规则
        query.setSort("id",SolrQuery.ORDER.desc);
        //设置返回格式
        query.set("wt","json");
        ////设置分页
        query.set("start", (currentPage - 1) * 10);
        query.set("rows", 10);
        // 进行查询得到返回结果
        QueryResponse queryResponse = solrClient.query(query);
       //        取出高亮你在部分
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        //得到主体数据部分
        SolrDocumentList results = queryResponse.getResults();

        ArrayList<FilmInfo> newsList = new ArrayList<>();
        // 对主体数据进行遍历，将数据依次保存到news对象中，然后将news对象加入list集合就是查询到的所有新闻
        for (SolrDocument result : results){
            FilmInfo news = FilmInfo.builder().id(result.get("id").toString())
                    .filmName(result.get("filmName").toString())
                    .filmAlias(result.get("filmAlias") == null ? "" : result.get("filmAlias").toString())
                    .filmPoster(result.get("filmPoster").toString())
                    .filmImg(result.get("filmImg").toString())
                    .filmVideo(result.get("filmVideo") == null ? "" : result.get("filmVideo").toString())
                    .filmRegisseur(result.get("filmRegisseur").toString())
                    .filmWriter(result.get("filmWriter").toString())
                    .filmLeading(result.get("filmLeading").toString())
                    .filmDeartion(Integer.parseInt(result.get("filmDeartion").toString()))
                    .filmDesc(result.get("filmDesc").toString())
                    .filmViewFocus(result.get("filmViewFocus") == null ? "" : result.get("filmViewFocus").toString())
                .build();

            //设置高亮部分，下边是得到指定新闻id的高亮部分，并且将高亮部分设置进入对象中
            Map<String, List<String>> map = highlighting.get(result.get("id"));
            List<String> list = map.get("filmName");
            if(null != list && list.size() > 0){
                String filmName = list.get(0);
                news.setFilmName(filmName);
            }
            List<String> list1 = map.get("filmDesc");
            if(null != list1 && list1.size() > 0){
                String filmDesc = list1.get(0);
                news.setFilmDesc(filmDesc);
            }
            newsList.add(news);
        }

        //得到所获得的新闻条数
        long numFound = results.getNumFound();
        // 下边是我自己的分页封装，可忽略，上边的到的newslist就是获得的所有新闻
        PageResult result = new PageResult();
        result.setTotal(numFound);
        result.setResultData(new GenericPage<FilmInfo>(currentPage, 10, newsList, numFound));
        return result;
    }
}
